export default {
	// inject: ['importExcelShow'],
	data() {
		return {
			page: {
				pageSize: 10, //偏移，页面数据个数
				pageSizes: [10, 20, 50],
				pageNo: 1, //当前页数
				total: 0, //总数据量
				sizeChange: this.handleSizeChange,
				currentChange: this.handleCurrentChange
			},
			loading: false,
			sortAttr: {
				sort: null,
				dir: null
			}, //升序，降序
			searchcondition: {},
			tableData: [],
			tableStatistics: {},
			showSearch: false,
			selectIds: [], // 批量删除-已选择项
			importExportShow: false,
			tableNum: 0,
			pickerOptions:{
				disabledDate(time) {
					return time.getTime() > Date.now()
				}
			}
		}
	},
	watch: {
		'page.pageNo': function(val) {
			this.getList()
		},
		'page.pageSize': function(val) {
			this.getList()
		},
		'$route.query': function(val) {
			if(val.reflist) {
				this.refresh()
			}
		}
	},
	mounted() {
		// this.$bus.$on('refresh', () => {
		// 	this.refresh();
		// })
	},
	created() {
		this.keyupSubmit();
	},
	methods: {
		keyupSubmit() { // 搜索绑定回车键
			if (this.listUrl) {
			  document.onkeydown = (e)=> {
				const _key = e.keyCode
				if (_key === 13 && this.$route.path.indexOf('list') != -1) {
					this.refresh()
				}
			  }
			}
		},
		handleReset() { // 清空搜索选项
			this.searchcondition = {};
			this.resetOhter();
			this.refresh();
		},
		resetOhter(){},
		handleDel(row) {
			this.$confirm('确认删除该条记录吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let params={
					id:row.id
				}
				this.httptool.post(this.delUrl, params).then((res)=>{
					this.$message.success('删除成功!');
					this.refresh()
				})
			}).catch(()=>{})
		},
		handleDelAll() {
			if (this.selectIds.length <= 0) {
				this.$message.warning('请选择要删除的数据');
				return;
			}
			this.$confirm('确认删除吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let ids = []
				this.selectIds.forEach(v => {
					ids.push(v.id)
				})
				let params={
					id:ids.join(',')
				}
				this.httptool.post(this.delAllUrl, params).then((res)=>{
					this.$message.success('删除成功!');
					this.refresh()
				})
			})
		},
		handleSelectionChange(val) {// 勾选的数据
			this.selectIds = val
		},
		handleSizeChange(val) {
			this.page.pageSize = val
		},
		handleCurrentChange(val) {
			this.page.pageNo = val
		},
		refresh(other) {
			if(this.listUrl) {
				this.page.pageNo = 1;
				this.getList(other);
				this.getListTotals();
			}
		},
		getListTotals() {
			if(!this.listTotalUrl) return;
			this.loading = true;
			this.searchcondition.pageNo = this.page.pageNo;
			this.searchcondition.pageSize = this.page.pageSize;
			// 列表时间段搜索
			if(this.date && this.date.length>0) {
				this.searchcondition.startDate = this.date[0]
				this.searchcondition.endDate = this.date[1]
			}
			for(let key in this.searchcondition){
				if(this.searchcondition[key] === ''){
					this.searchcondition[key] = undefined
				}
			}
			this.httptool.post(this.listTotalUrl, this.searchcondition).then(res => {
				this.loading = false;
				this.tableStatistics=res.data;
			}, () => {
				this.loading = false;
			})
		},
		getList(other) {
			if(!this.listUrl) return;
			this.loading = true;
			this.searchcondition.pageNo = this.page.pageNo;
			this.searchcondition.pageSize = this.page.pageSize;
			
			// 列表时间段搜索
			if(this.date && this.date.length>0) {
				this.searchcondition.startDate = this.date[0]
				this.searchcondition.endDate = this.date[1]
			}
			for(let key in this.searchcondition){
				if(this.searchcondition[key] === ''){
					this.searchcondition[key] = undefined
				}
			}
			this.httptool.post(this.listUrl, this.searchcondition).then(res => {
				this.loading = false;
				if(!res.result) {
					res.result = {};
					res.result.content = [];
				}
				if(other){
					this[other] = res.result[other];
				}
				this.tableData = res.result.content;
				this.tableNum = res.result.total || 0
				this.page.total = res.result.total;
			}, () => {
				this.loading = false;
			})
		},
		tableSort(val) {
			let isAttr = (/^_/.exec(val.prop) || [])[0];
			this.sortAttr.sort = isAttr ? val.prop.substr(1) : val.prop;
			if(val.order === 'ascending') {
				this.sortAttr.dir = 'asc';
			} else if(val.order === 'descending') {
				this.sortAttr.dir = 'desc'
			} else {
				this.sortAttr.dir = val.order
			}
			this.refresh()
		},
		handleExport() {
			this.searchcondition.pageNo = this.page.pageNo;
			this.searchcondition.pageSize = this.page.pageSize;
			for(let key in this.searchcondition){
				if(this.searchcondition[key] === ''){
					this.searchcondition[key] = undefined
				}
			}
			this.$message.warning('正在导出！');
			this.httptool.post({
				url: this.exportUrl,
				responseType: 'blob',
				data:this.searchcondition
			}).then(res=>{
				this.$message.success('导出成功！');
				const a = document.createElement('a');
				let blob = new Blob([res]);
				a.style.display = 'none';
				a.href = URL.createObjectURL(blob);
				a.setAttribute('download',this.$route.meta.title+'.xlsx')
				document.body.appendChild(a)
				a.click();
				document.body.removeChild(a);
			})
		},
		handleImportSuccess(){
			this.getList()
		}
	},
	activated(){
		this.getList();
	}
}